package net.yyge.manifest.editor.tag;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import java.util.LinkedHashMap;
import java.util.Map;

public class ProviderTag extends XmlTag{
    Map<String, Element> data = new LinkedHashMap<>();
    private Document document = null;


    public ProviderTag(Document document) {
        this.document = document;
    }

    public ProviderTag initProvider(Element element) {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);
            if ("android:authorities".equals(attribute.getNodeName())) {
                data.put(attribute.getNodeValue(), element);
            }
        }
        return this;
    }

    public ProviderTag updateNamePackage(String oldPackageName, String packageName) {
        for (String key : data.keySet()){
            Element element = data.get(key);
            String text = getAttrValue(element.getAttributes(), "android:authorities");
            if(text.contains(oldPackageName)){
                String replace = text.replace(oldPackageName, packageName);
                setValue(element,"android:authorities",replace);
            }
        }
        return this;
    }
}
